package com.zys.el.search.aspect;

import org.apache.http.HttpHost;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Aspect
@Component
public class AnticleAspect {

    private RestHighLevelClient client;

    @Around("execution(* com.zys.el.search.dao.AnticleDao+.*(..))")
    public Object around(ProceedingJoinPoint pj) throws IOException {
        Object proceed = null;
        try {
            HttpHost host = HttpHost.create("http://120.78.130.50:9200");//"http://120.78.130.50:9200" http://114.96.92.168:9200
            RestClientBuilder builder = RestClient.builder(host);
            client = new RestHighLevelClient(builder);
            proceed = pj.proceed();
            return proceed;
        } catch (Throwable e) {
            e.printStackTrace();
        } finally {
            client.close();
        }

        return proceed;
    }

    public RestHighLevelClient getClient() {
        return this.client;
    }
}
